package com.zsrt.userDAO;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import com.zsrt.Entity.UserEntity;
import com.zsrt.Utils.JDBC_Utils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Dao {
	private Connection con = null;
	public Dao(String dbType) {
		if ("mysql".equals(dbType))
			con = JDBC_Utils.getConnections("mysql_jdbc.properties");
		else if ("oracle".equals(dbType))
			con = JDBC_Utils.getConnections("oracle_jdbc.properties");
	}
	public boolean add(UserEntity st) { // update操作
		String sql = "insert into crm_user_t (USER_CELLPHONE,PASSWORD) values (?,?)";
		PreparedStatement ps = null;
		boolean flag=false;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, st.getUserCellphone());
			ps.setString(2, st.getPassword());
			ps.executeUpdate();
			flag=true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBC_Utils.close(ps);
			JDBC_Utils.close(con);
		}
		return flag;
	}
	public void call(int x, int y) { // 存储过程调用
		String sql = "call gogogo(?,?,?)";
		CallableStatement cs = null;
		try {
			cs = con.prepareCall(sql);
			cs.setInt(1, x);
			cs.setInt(2, y);
			cs.registerOutParameter(3, Types.INTEGER);
			cs.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBC_Utils.close(cs);
			JDBC_Utils.close(con);
		}
	}
	/*
	 * public List<StudentEntity> query(String sql) { // 查询操作
	 * List<StudentEntity> list = new ArrayList<>(); PreparedStatement ps =
	 * null; try { ps = con.prepareStatement(sql); ResultSet rs =
	 * ps.executeQuery(); while (rs.next()) { StudentEntity stu = new
	 * StudentEntity(); stu.setAge(rs.getInt("AGE"));
	 * stu.setName(rs.getString("NAME")); list.add(stu); } } catch (SQLException
	 * e) { e.printStackTrace(); } finally { JDBC_Utils.close(ps);
	 * JDBC_Utils.close(con); } return list; }
	 */
	public UserEntity query(String cellphone) { // 查询操作
		PreparedStatement ps = null;
		String sql = "select *from crm_user_t where USER_CELLPHONE="+cellphone;
		UserEntity stu = new UserEntity();
		try {
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				stu.setId(rs.getInt(1));
				stu.setImg(rs.getString(4));
				stu.setPassword(rs.getString(3));
				stu.setUserCellphone(rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBC_Utils.close(ps);
			JDBC_Utils.close(con);
		}
		return stu;
	}
}
